<?php declare(strict_types=1);

/**
 * @package HelloPortal (Light Portal)
 * @link https://custom.simplemachines.org/index.php?mod=4244
 * @author Bugo <bugo@dragomano.ru>
 * @copyright 2021-2025 Bugo
 * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later
 *
 * @category plugin
 * @version 09.11.25
 */

namespace LightPortal\Plugins\HelloPortal;

use Bugo\Compat\Lang;
use Bugo\Compat\Theme;
use Bugo\Compat\Utils;
use LightPortal\Enums\ForumHook;
use LightPortal\Plugins\Event;
use LightPortal\Plugins\Plugin;
use LightPortal\Plugins\PluginAttribute;
use LightPortal\Plugins\SettingsFactory;

if (! defined('LP_NAME'))
	die('No direct access...');

/**
 * Generated by PluginMaker
 */
#[PluginAttribute]
class HelloPortal extends Plugin
{
	private array $themes = ['', 'dark', 'modern', 'flattener'];

	private readonly ?string $sa;

	private readonly string $steps;

	public function __construct()
	{
		parent::__construct();

		$this->sa = $this->request()->get('sa');

		$this->steps = $this->getStepData();
	}

	public function init(): void
	{
		$this->applyHook(ForumHook::menuButtons);
	}

	public function menuButtons(): void
	{
		if (! $this->canShowTourButton())
			return;

		Lang::load('Post');

		Utils::$context['lp_reference_links'][] = [
			'icon'   => 'fas fa-circle-info',
			'text'   => Lang::$txt['lp_hello_portal']['tour_button'],
			'params' => 'x-on:click.prevent="runTour()" x-data',
		];

		$resources = [
			['type' => 'css', 'url' => 'https://cdn.jsdelivr.net/npm/intro.js@4/minified/introjs.min.css'],
		];

		if (! empty($this->context['theme'])) {
			$theme = $this->context['theme'] . '.css';
			$resources[] = ['type' => 'css', 'url' => 'https://cdn.jsdelivr.net/npm/intro.js@4/themes/introjs-' . $theme];
		}

		if (Utils::$context['right_to_left']) {
			$resources[] = ['type' => 'css', 'url' => 'https://cdn.jsdelivr.net/npm/intro.js@4/minified/introjs-rtl.min.css'];
		}

		$resources[] = ['type' => 'js', 'url' => 'https://cdn.jsdelivr.net/npm/intro.js@4/minified/intro.min.js'];

		$this->loadExternalResources($resources);

		Theme::addInlineJavaScript('
		function runTour() {
			introJs().setOptions({
				tooltipClass: "lp_addon_hello_portal",
				nextLabel: ' . Utils::escapeJavaScript(Lang::$txt['admin_next']) . ',
				prevLabel: ' . Utils::escapeJavaScript(Lang::$txt['back']) . ',
				doneLabel: ' . Utils::escapeJavaScript(Lang::$txt['attach_dir_ok']) . ',
				steps: [' . $this->steps . '],
				showProgress: ' . (
			empty($this->context['show_progress']) ? 'false' : 'true'
			) . ',
				showButtons: ' . (
			empty($this->context['show_buttons']) ? 'false' : 'true'
			) . ',
				showBullets: false,
				exitOnOverlayClick: ' . (
			empty($this->context['exit_on_overlay_click']) ? 'false' : 'true'
			) . ',
				keyboardNavigation: ' . (
			empty($this->context['keyboard_navigation']) ? 'false' : 'true'
			) . ',
				disableInteraction: ' . (
			empty($this->context['disable_interaction']) ? 'false' : 'true'
			) . ',
				scrollToElement: true,
				scrollTo: "tooltip"
			}).start();
		}');
	}

	public function addSettings(Event $e): void
	{
		$e->args->settings[$this->name] = SettingsFactory::make()
			->select('theme', array_combine($this->themes, $this->txt['theme_set']))
			->check('show_progress')
			->check('show_buttons')
			->check('exit_on_overlay_click')
			->check('keyboard_navigation')
			->check('disable_interaction')
			->toArray();
	}

	public function credits(Event $e): void
	{
		$e->args->links[] = [
			'title' => 'Intro.js',
			'link' => 'https://github.com/usablica/intro.js',
			'author' => 'Afshin Mehrabani',
			'license' => [
				'name' => 'GNU AGPLv3',
				'link' => 'https://github.com/usablica/intro.js/blob/master/license.md'
			]
		];
	}

	private function getStepData(): string
	{
		$steps = (new TourRegistry())->getSteps($this->txt);

		return match (true) {
			$this->isCurrentArea('lp_settings', 'basic') => $steps['basic_settings'],
			$this->isCurrentArea('lp_settings', 'extra', false) => $steps['extra_settings'],
			$this->isCurrentArea('lp_settings', 'panels', false) => $steps['panels'],
			$this->isCurrentArea('lp_settings', 'misc', false) => $steps['misc'],
			$this->isCurrentArea('lp_blocks') => $steps['blocks'],
			$this->isCurrentArea('lp_pages') => $steps['pages'],
			$this->isCurrentArea('lp_categories') => $steps['categories'],
			$this->isCurrentArea('lp_plugins') => $steps['plugins'],
			$this->isCurrentArea('lp_plugins', 'add', false) => $steps['add_plugins'],
			default => ''
		};
	}

	private function isCurrentArea(string $area, string $sa = 'main', bool $canBeEmpty = true): bool
	{
		return $this->request()->has('area') && $this->request()->get('area') === $area &&
			(
			$canBeEmpty
				? ($this->sa === $sa || empty($this->sa))
				: $this->sa === $sa
			);
	}

	private function canShowTourButton(): bool
	{
		if ($this->request()->isNot('admin') || empty($this->steps)) {
			return false;
		}

		if (empty($this->request()->get('area')) || empty(Utils::$context['template_layers'])) {
			return false;
		}

		return true;
	}
}
